<?php
namespace app\beautiful\model\member;
use app\beautiful\model\LevelModel;
use app\beautiful\model\StaffMemberModel;
use app\common\model\CommonModel;

class MemberModel extends CommonModel{
    protected  $name = 'beautiful_member';
    protected  $pk   = 'member_id';

    public function level()
    {
        return $this->hasOne(LevelModel::class,'level_id','level_id');
    }

    public function balance()
    {
        return $this->hasMany(MemberBalanceModel::class,'member_id','member_id');
    }

    public function integral()
    {
        return $this->hasMany(MemberIntegralModel::class,'member_id','member_id');
    }

    public function adviser()
    {
        return $this->hasOne(StaffMemberModel::class,'member_id','member_id');
    }

    public function getBirthdayAttr($value,$data)
    {   
        $my = $data['birthday_year'].'-'.$data['birthday_month'].'-'.$data['birthday_day'];
        return empty( $data['birthday_year']) ? '' : $my;
    }

    public function getHiddenMobileAttr($value,$data)
    {
        $mobile=$data['mobile'] ?? '';
        return yc_phone($mobile);
    }

    public function getMemberInfoByMemberId($memberIds,$type = 0){
        $where = [
            ['member_id','IN',$memberIds],
        ];

        $datas = $this->field('member_id,face,nick_name,mobile')->where($where)->select()->toArray();
        $return = [];
        foreach($datas as $val){
            $return[$val['member_id']] = $val;
        }
        return $return;
    }

}
